﻿namespace Chain.Wechat
{
    using Chain.BLL;
    using Chain.Common;
    using Chain.Common.DEncrypt;
    using Chain.Model;
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Text.RegularExpressions;
    using System.Xml;

    public class TextRequest : IRequest
    {
        
        private string _Content;
        
        private string _MsgId;

        public TextRequest(XmlDocument input) : base(input)
        {
            XmlNode root = base.XmlDoc.FirstChild;
            this.Content = root["Content"].InnerText.Trim();
            this.MsgId = root["MsgId"].InnerText;
            base.RequestEvent += new IRequest.RequestEventHandler(this.TextReqeustEventHandler);
        }

        private bool CheckMobileFormat(string mobile)
        {
            return Regex.IsMatch(mobile, @"^1[3-9]\d{9}$");
        }

        private string GetRandomCode()
        {
            Random random = new Random();
            string result = string.Empty;
            for (int i = 0; i < 4; i++)
            {
                result = result + random.Next(0, 10);
            }
            return result;
        }

        private string RegisterEntryCode(RegisterEntryCodeLog codeLog)
        {
            codeLog.WrningCount++;
            if (codeLog.RandomCode == this.Content)
            {
                LogManager.Remove(base.FromUserName);
                return this.TryRegisterMem(codeLog.MobileNumber, base.FromUserName);
            }
            if (codeLog.WrningCount >= 3)
            {
                LogManager.Remove(base.FromUserName);
                return new TextResponse(this, "由于您的误操作次数过多,会员申请模式已退出!").Result();
            }
            LogManager.Add(base.FromUserName, codeLog);
            return new TextResponse(this, "您发送的验证码不正确,请重新发送!").Result();
        }

        private string RegisterEntryPhone(RegisterEntryNumberLog regLog)
        {
            string randomCode;
            regLog.WrningCount++;
            if (!this.CheckMobileFormat(this.Content))
            {
                if (regLog.WrningCount >= 3)
                {
                    LogManager.Remove(base.FromUserName);
                    return new TextResponse(this, "由于您的误操作次数过多,会员申请模式已退出!").Result();
                }
                LogManager.Add(base.FromUserName, regLog);
                return new TextResponse(this, "您发送的手机号格式有误,请重新发送!").Result();
            }
            if (new Chain.BLL.Mem().GetModelByMemMobile(this.Content) != null)
            {
                if (regLog.WrningCount >= 3)
                {
                    LogManager.Remove(base.FromUserName);
                    return new TextResponse(this, "由于您的误操作次数过多,会员申请模式已退出!").Result();
                }
                LogManager.Add(base.FromUserName, regLog);
                return new TextResponse(this, "该手机号已被注册,请重新输入一个新的手机号!").Result();
            }
            if (this.SendRandomCode(base.FromUserName, this.Content, out randomCode))
            {
                RegisterEntryCodeLog codeLog = new RegisterEntryCodeLog(0, randomCode, this.Content);
                LogManager.Add(base.FromUserName, codeLog);
                return new TextResponse(this, "温馨提示,已经向您的手机发送了验证码,请输入验证码确认手机号码!").Result();
            }
            LogManager.Remove(base.FromUserName);
            return this.TryRegisterMem(this.Content, base.FromUserName);
        }

        private bool SendRandomCode(string openId, string mobile, out string randomCode)
        {
            Chain.Model.SysParameter modelSysPara = new Chain.BLL.SysParameter().GetModel(1);
            if (((modelSysPara != null) && modelSysPara.WeiXinSMSVcode) && (modelSysPara.Sms && this.CheckMobileFormat(mobile)))
            {
                SMSHelper sms = new SMSHelper(4, modelSysPara.SmsSeries, modelSysPara.SmsSerialPwd);
                if (sms.GetBalance() > 0)
                {
                    randomCode = this.GetRandomCode();
                    string notify = "温馨提示,欢迎您注册" + modelSysPara.WeiXinShopName + "微信会员,您的" + modelSysPara.WeiXinShopName + "微信会员短信验证码是：" + randomCode;
                    if (sms.Send_GXSMS(mobile, notify, ""))
                    {
                        Chain.Model.SmsLog modelSms = new Chain.Model.SmsLog();
                        modelSms.SmsMemID = 0;
                        modelSms.SmsMobile = this.Content;
                        modelSms.SmsContent = notify;
                        modelSms.SmsTime = DateTime.Now;
                        modelSms.SmsShopID = 1;
                        modelSms.SmsUserID = 1;
                        modelSms.SmsAmount = 1;
                        modelSms.SmsAllAmount = modelSms.SmsAmount;
                        new Chain.BLL.SmsLog().Add(modelSms);
                        return true;
                    }
                }
            }
            randomCode = "";
            return false;
        }

        public string TextReqeustEventHandler()
        {
            if (!LogManager.ContainsKey(base.FromUserName))
            {
                if (this.Content == "会员")
                {
                    Chain.Model.Mem mem = new Chain.BLL.Mem().GetMemByWeiXinCard(base.FromUserName);
                    if (mem != null)
                    {
                        return new BusinessLogic().GetMemCardResponse(this, mem, "");
                    }
                    RegisterEntryNumberLog log = new RegisterEntryNumberLog(0);
                    LogManager.Add(base.FromUserName, log);
                    return new TextResponse(this, "您已进入会员申请模式,请发送您的手机号码!").Result();
                }
                if (this.Content == "绑定")
                {
                    if (new Chain.BLL.Mem().GetMemByWeiXinCard(base.FromUserName) != null)
                    {
                        return new TextResponse(this, "您已经绑定了微信会员!").Result();
                    }
                    TransformEntryNumberLog log = new TransformEntryNumberLog(0);
                    LogManager.Add(base.FromUserName, log);
                    return new TextResponse(this, "您已进入转微信会员模式,请发送您的卡号!").Result();
                }
                if (!(this.Content == "签到"))
                {
                    string responseText = new BusinessLogic().GetResponseByRule(this, this.Content);
                    if (!string.IsNullOrEmpty(responseText))
                    {
                        return responseText;
                    }
                    return new TextResponse(this, new BusinessLogic().GetWelcomeText()).Result();
                }
                Chain.BLL.Mem bllMem = new Chain.BLL.Mem();
                Chain.Model.Mem mem1 = bllMem.GetMemByWeiXinCard(base.FromUserName);
                if (mem1 == null)
                {
                    return new TextResponse(this, "您还未申请或绑定会员卡!").Result();
                }
                switch (new BusinessLogic().MemSign(mem1))
                {
                    case -3:
                        return new TextResponse(this, "您今日已经签到过，重复签到不会再次赠送积分!").Result();

                    case -2:
                        return new TextResponse(this, "对不起，签到送积分活动未开启!").Result();

                    case -1:
                        return new TextResponse(this, "您还未申请或绑定会员卡!").Result();

                    case 0:
                        goto Label_024B;

                    case 1:
                        mem1 = bllMem.GetMemByWeiXinCard(base.FromUserName);
                        return new BusinessLogic().GetMemCardResponse(this, mem1, "今日签到成功!");
                }
            }
            else
            {
                ILog log = LogManager.Get(base.FromUserName);
                switch (log.LogType)
                {
                    case "RegisterEntryNumberLog":
                    {
                        RegisterEntryNumberLog regLog = (RegisterEntryNumberLog) log;
                        return this.RegisterEntryPhone(regLog);
                    }
                    case "RegisterEntryCodeLog":
                    {
                        RegisterEntryCodeLog codeLog = (RegisterEntryCodeLog) log;
                        return this.RegisterEntryCode(codeLog);
                    }
                    case "TransformEntryNumberLog":
                    {
                        TransformEntryNumberLog transformNumberLog = (TransformEntryNumberLog) log;
                        return this.TransformEntryNumber(transformNumberLog);
                    }
                    case "TransformEntryCodeLog":
                    {
                        TransformEntryCodeLog transformCodeLog = (TransformEntryCodeLog) log;
                        return this.TransformEntryCode(transformCodeLog);
                    }
                    case "TransformEntryPwdLog":
                    {
                        TransformEntryPwdLog transformPwdLog = (TransformEntryPwdLog) log;
                        return this.TransformEntryPwd(transformPwdLog);
                    }
                }
                LogManager.Remove(base.FromUserName);
                return string.Empty;
            }
        Label_024B:
            return new TextResponse(this, "系统出现未知错误!").Result();
        }

        private string TransformEntryCode(TransformEntryCodeLog transformCodeLog)
        {
            transformCodeLog.WrningCount++;
            if (transformCodeLog.RandomCode == this.Content)
            {
                LogManager.Remove(base.FromUserName);
                Chain.BLL.Mem bllMem = new Chain.BLL.Mem();
                Chain.Model.Mem mem = bllMem.GetModelByMemCard(transformCodeLog.MemCard);
                mem.MemWeiXinCard = base.FromUserName;
                bllMem.Update(mem);
                return new BusinessLogic().GetMemCardResponse(this, mem, "绑定微信会员卡成功!");
            }
            if (transformCodeLog.WrningCount >= 3)
            {
                LogManager.Remove(base.FromUserName);
                return new TextResponse(this, "由于您的误操作次数过多,会员转微会员模式已退出!").Result();
            }
            LogManager.Add(base.FromUserName, transformCodeLog);
            return new TextResponse(this, "您发送的验证码不正确,请重新发送!").Result();
        }

        private string TransformEntryNumber(TransformEntryNumberLog transformLog)
        {
            string randomCode;
            transformLog.WrningCount++;
            Chain.Model.Mem mem = new Chain.BLL.Mem().GetModelByMemCard(this.Content);
            if (mem == null)
            {
                if (transformLog.WrningCount >= 3)
                {
                    LogManager.Remove(base.FromUserName);
                    return new TextResponse(this, "由于您的误操作次数过多,会员转微会员模式已退出!").Result();
                }
                LogManager.Add(base.FromUserName, transformLog);
                return new TextResponse(this, "没有查找到您输入的卡号,请重新输入一个正确的卡号!").Result();
            }
            if (this.SendRandomCode(base.FromUserName, mem.MemMobile, out randomCode))
            {
                TransformEntryCodeLog transformCodeLog = new TransformEntryCodeLog(0, randomCode, this.Content);
                LogManager.Add(base.FromUserName, transformCodeLog);
                return new TextResponse(this, "温馨提示,已经向您的手机发送了验证码,请回复收到的验证码确认手机号码!").Result();
            }
            TransformEntryPwdLog transformPwdLog = new TransformEntryPwdLog(0, this.Content);
            LogManager.Add(base.FromUserName, transformPwdLog);
            return new TextResponse(this, "温馨提示,已查找到对应的卡号,请回复会员卡密码!无密码请回复#").Result();
        }

        private string TransformEntryPwd(TransformEntryPwdLog transformPwdLog)
        {
            transformPwdLog.WrningCount++;
            string pwd = (this.Content == "#") ? "" : ((this.Content == "＃") ? "" : this.Content);
            pwd = DESEncrypt.Encrypt(pwd);
            Chain.BLL.Mem bllMem = new Chain.BLL.Mem();
            Chain.Model.Mem mem = bllMem.CheckMemPwd(transformPwdLog.MemCard, pwd);
            if (mem != null)
            {
                LogManager.Remove(base.FromUserName);
                mem.MemWeiXinCard = base.FromUserName;
                mem.MemWeiXinCards = base.FromUserName;
                mem.MemAttention = 1;
                bllMem.Update(mem);
                return new BusinessLogic().GetMemCardResponse(this, mem, "绑定微信会员卡成功!");
            }
            if (transformPwdLog.WrningCount >= 3)
            {
                LogManager.Remove(base.FromUserName);
                return new TextResponse(this, "由于您的误操作次数过多,会员转微会员模式已退出!").Result();
            }
            LogManager.Add(base.FromUserName, transformPwdLog);
            return new TextResponse(this, "您发送的密码不正确,请重新发送!").Result();
        }

        private string TryRegisterMem(string mobile, string openID)
        {
            int status = new BusinessLogic().MemRegister(mobile, openID);
            if (status > 0)
            {
                Chain.Model.Mem mem = new Chain.BLL.Mem().GetModel(status);
                string show = "注册微信会员卡成功\r\n会员卡密码默认为手机后6位数\r\n请及时登陆会员自助平台进行修改！";
                return new BusinessLogic().GetMemCardResponse(this, mem, show);
            }
            switch (status)
            {
                case -1:
                    return new TextResponse(this, "会员卡号重复,会员办卡失败!").Result();

                case -2:
                    return new TextResponse(this, "手机号码重复,会员办卡失败!").Result();

                case -6:
                    return new TextResponse(this, "卡面号重复,会员办卡失败!").Result();
            }
            return new TextResponse(this, "未知原因,办卡失败!").Result();
        }

        public string Content
        {
            
            get
            {
                return this._Content;
            }
            
            set
            {
                this._Content = value;
            }
        }

        public string MsgId
        {
            
            get
            {
                return this._MsgId;
            }
            
            set
            {
                this._MsgId = value;
            }
        }
    }
}

